using SS;
using System;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Windows.Forms;
namespace TVSGPP.func
{
	public class frmSavePlan : Form
	{
		public DataTable objTable = new DataTable();
		public bool saved = false;
		private IContainer components = null;
		private Label label1;
		private TextBox txtDateCreate;
		private TextBox txtNote;
		private Label label2;
		private Button btnSave;
		private Button btnClose;
		public frmSavePlan()
		{
			this.InitializeComponent();
		}
		private void frmSavePlan_Load(object sender, EventArgs e)
		{
			this.txtDateCreate.Text = DateTime.Now.ToShortDateString();
		}
		private void frmSavePlan_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyCode == Keys.Escape)
			{
				base.Close();
				this.saved = false;
			}
		}
		private void btnClose_Click(object sender, EventArgs e)
		{
			base.Close();
			this.saved = false;
		}
		private int makePlan()
		{
			int result = -1;
			try
			{
				SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
				sqlConnection.Open();
				SqlCommand sqlCommand = sqlConnection.CreateCommand();
				sqlCommand.CommandText = "INSERT INTO [TabProductPlan](DateCreate,Note) VALUES(getdate(),'" + this.txtNote.Text + "')";
				sqlCommand.ExecuteNonQuery();
				SqlCommand sqlCommand2 = sqlConnection.CreateCommand();
				sqlCommand2.CommandText = "SELECT TOP 1 AutoItem FROM TabProductPlan ORDER BY AutoItem DESC";
				SqlDataReader sqlDataReader = sqlCommand2.ExecuteReader();
				while (sqlDataReader.Read())
				{
					try
					{
						result = int.Parse(sqlDataReader["AutoItem"].ToString());
					}
					catch
					{
						result = -1;
					}
				}
				sqlDataReader.Close();
				sqlConnection.Close();
				sqlConnection.Dispose();
			}
			catch
			{
			}
			return result;
		}
		private void btnSave_Click(object sender, EventArgs e)
		{
			try
			{
				int num = this.makePlan();
				SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
				sqlConnection.Open();
				SqlCommand sqlCommand = sqlConnection.CreateCommand();
				for (int i = 0; i < this.objTable.Rows.Count; i++)
				{
					string text = "INSERT INTO [TabProductPlanDetail]([PlanID],[ProductName],[ProductUnit],[numFirstBalancer],[ProductInputInTime],[ProductTotal],[ProductOutputInTime],[ProductYesNo],[ProductBalancer],[NumPlan]) ";
					string text2 = text;
					text = string.Concat(new string[]
					{
						text2,
						" VALUES(",
						num.ToString(),
						",N'",
						this.objTable.Rows[i]["ProductName"].ToString(),
						"',N'",
						this.objTable.Rows[i]["ProductUnit"].ToString(),
						"',",
						this.objTable.Rows[i]["numFirstBalancer"].ToString(),
						",",
						this.objTable.Rows[i]["ProductInputInTime"].ToString(),
						",",
						this.objTable.Rows[i]["ProductTotal"].ToString(),
						",",
						this.objTable.Rows[i]["ProductOutputInTime"].ToString(),
						",",
						this.objTable.Rows[i]["ProductYesNo"].ToString(),
						",",
						this.objTable.Rows[i]["ProductBalancer"].ToString(),
						",",
						this.objTable.Rows[i]["NumPlan"].ToString(),
						")"
					});
					sqlCommand.CommandText = text;
					sqlCommand.ExecuteNonQuery();
				}
				sqlConnection.Close();
				sqlConnection.Dispose();
				this.saved = true;
				base.Close();
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
			}
		}
		protected override void Dispose(bool disposing)
		{
			if (disposing && this.components != null)
			{
				this.components.Dispose();
			}
			base.Dispose(disposing);
		}
		private void InitializeComponent()
		{
			this.label1 = new Label();
			this.txtDateCreate = new TextBox();
			this.txtNote = new TextBox();
			this.label2 = new Label();
			this.btnSave = new Button();
			this.btnClose = new Button();
			base.SuspendLayout();
			this.label1.AutoSize = true;
			this.label1.Location = new Point(27, 25);
			this.label1.Name = "label1";
			this.label1.Size = new Size(61, 15);
			this.label1.TabIndex = 0;
			this.label1.Text = "Ngày lập :";
			this.txtDateCreate.BackColor = Color.White;
			this.txtDateCreate.Location = new Point(92, 23);
			this.txtDateCreate.Name = "txtDateCreate";
			this.txtDateCreate.ReadOnly = true;
			this.txtDateCreate.Size = new Size(210, 21);
			this.txtDateCreate.TabIndex = 19000;
			this.txtNote.Location = new Point(92, 50);
			this.txtNote.Name = "txtNote";
			this.txtNote.Size = new Size(210, 21);
			this.txtNote.TabIndex = 0;
			this.label2.AutoSize = true;
			this.label2.Location = new Point(27, 52);
			this.label2.Name = "label2";
			this.label2.Size = new Size(62, 15);
			this.label2.TabIndex = 2;
			this.label2.Text = "Diễn giải :";
			this.btnSave.Cursor = Cursors.Hand;
			this.btnSave.Font = new Font("Arial", 9f, FontStyle.Bold, GraphicsUnit.Point, 163);
			this.btnSave.Location = new Point(73, 90);
			this.btnSave.Name = "btnSave";
			this.btnSave.Size = new Size(90, 30);
			this.btnSave.TabIndex = 1;
			this.btnSave.Text = "Ghi nhận";
			this.btnSave.UseVisualStyleBackColor = true;
			this.btnSave.Click += new EventHandler(this.btnSave_Click);
			this.btnClose.Cursor = Cursors.Hand;
			this.btnClose.Font = new Font("Arial", 9f, FontStyle.Bold, GraphicsUnit.Point, 163);
			this.btnClose.Location = new Point(169, 90);
			this.btnClose.Name = "btnClose";
			this.btnClose.Size = new Size(90, 30);
			this.btnClose.TabIndex = 2;
			this.btnClose.Text = "Thoát [ ESC ]";
			this.btnClose.UseVisualStyleBackColor = true;
			this.btnClose.Click += new EventHandler(this.btnClose_Click);
			base.AutoScaleDimensions = new SizeF(7f, 15f);
			//base.AutoScaleMode = AutoScaleMode.Font;
			base.ClientSize = new Size(331, 132);
			base.Controls.Add(this.btnClose);
			base.Controls.Add(this.btnSave);
			base.Controls.Add(this.txtNote);
			base.Controls.Add(this.label2);
			base.Controls.Add(this.txtDateCreate);
			base.Controls.Add(this.label1);
			this.Font = new Font("Arial", 9f, FontStyle.Regular, GraphicsUnit.Point, 163);
			//base.Icon = (Icon)componentResourceManager.GetObject("$this.Icon");
			base.KeyPreview = true;
			this.MaximumSize = new Size(347, 166);
			this.MinimumSize = new Size(347, 166);
			base.Name = "frmSavePlan";
			base.ShowInTaskbar = false;
			base.StartPosition = FormStartPosition.CenterScreen;
			this.Text = " ..: Lập dự trù thuốc";
			base.Load += new EventHandler(this.frmSavePlan_Load);
			base.KeyDown += new KeyEventHandler(this.frmSavePlan_KeyDown);
			base.ResumeLayout(false);
			base.PerformLayout();
		}
	}
}
